//
//  YDMyContactsViewController.m
//  IngCard
//
//  Created by kf on 2017/8/13.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDMyContactsViewController.h"
#import "YDMyContactTableViewCell.h"
#import "YDContact.h"
#import "YDContactsManager.h"
#import "YDContactDetailViewController.h"


@interface YDMyContactsViewController ()
@end

@implementation YDMyContactsViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [THEME renderNavigationBar:self style:YDNavigationStyle_Blue];
    self.tableView.sectionIndexColor = UIColorBlue;
}

- (void) initDataSource
{
    [CONTACT getListOnComplete:^(BOOL success, NSString *msg) {
        
        if (success && (CONTACT.contacts.count > 0))
        {
            CONTACT.sortType = YDContactSort_name;
            [CONTACT sort];
            [CONTACT.contacts_sort enumerateObjectsUsingBlock:^(NSArray<YDContact *> * contants, NSUInteger sectionIndex, BOOL * _Nonnull stop) {
                
                [contants enumerateObjectsUsingBlock:^(YDContact * contact, NSUInteger row, BOOL * _Nonnull stop) {
                    [self.dataSource addDataSource:[YDMyContactCellDataSource class]
                                         atSection:sectionIndex
                                            config:^(YDMyContactCellDataSource* dataSource)
                     {
                         dataSource.userName = contact.username;
                         dataSource.userTitle = NSStringFormat(@"%@,%@",contact.company,contact.occupation);
                         
                         dataSource.headerImageUrl =  [NSURL URLWithString:contact.headImage];
                     }];
                }];
            }];
        }
    }];
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return CONTACT.indexArray_sort;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
//        [CONTACT.contacts removeObject: [self.dataSource dataSourceAtIndexPath:indexPath]];
//        NSMutableArray<YDContact *> *contactSection = [CONTACT.contacts_sort objectAtIndex:indexPath.section];
//        [contactSection removeObjectAtIndex:indexPath.row];
//        if (contactSection.count == 0)
//        {
//            [CONTACT.indexArray_sort removeObjectAtIndex:indexPath.section];
//        }
        
        [self.dataSource deleteDataSourceByIndexPath:indexPath];
    }
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    40029,40030,40031,40033,40036,40037,40038,40039
//    [CONTACT add:@[@"40029",@"40030",@"40031"] OnComplete:^(BOOL success, NSString *msg) {
//        self.dataSource = nil;
//        [self initDataSource];
//
//        [self.tableView reloadData];
//    }];
    
    YDContactDetailViewController *detailVC = [YDContactDetailViewController new];
    [self.navigationController pushViewController:detailVC animated:YES];
}
@end
